var express = require('express')
const multer = require('multer') // 中间件,处理multipart/form-data类型数据
const path = require('path')
// const uploadHander = multer({ dest: '/data/upload/images', preservePath: true })

//文件上传参数配置
const storage = multer.diskStorage({
  //文件存储路径
  // destination: path.join(__dirname,'../','public/images'),
  destination: '/data/upload/images',
  filename: (req, file, cb) => {
    let type = file.originalname.split('.')[1]
    cb(null, `${file.fieldname}-${Date.now().toString()}.${type}`)
  },
})

//文件大小
const limits = {
  fields: 10, //非文件字段数量
  fieldSize: 3000 * 1024, //文件的大小，（3000kb*1024）字节 b
  files: 1, //文件上传
}

const uploadHander = multer({ storage, limits })

var router = express.Router()
const user = require('../controller/user')
const article = require('../controller/article')
const upload = require('../controller/upload')

// 登录
router.post('/login', user.getUserInfoByName, user.login)
router.post('/register', user.getUserInfoIsByName, user.register)

// 文章新增 || 查询
router.post('/add-article', article.addArticle)
router.post('/search-article', article.searchArticle)
router.post('/get-page', article.getPrevNextArticle)

// 上传文件
router.post('/file-upload', uploadHander.single('file'), upload.upload)

module.exports = router
